#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
using namespace std;
int m, n, k;	
const int N = 1e7 + 10;
typedef pair<int,int> PII;
int e[N], ne[N], h[N], w[N];
int idx;		
long long dis[N], flag[N];
void add(int a, int b, int c)
{
	w[idx] = c;
	e[idx] = b;
	ne[idx] = h[a];
	h[a] = idx++;
}
void Dij()
{
	memset(dis, 0x3f, sizeof(dis));
	dis[k] = 0;
	priority_queue<PII, vector<PII>, greater<PII>>heap;
	heap.push({0,k});
	while (heap.size()) {
		PII temp = heap.top();
		heap.pop();
		int distance = temp.first;
		int vertex = temp.second;
		if (flag[vertex] == 1) {
			continue;
		}
		else {
			flag[vertex] = 1;
		}
		for (int i = h[vertex]; i != -1; i = ne[i]) {
			int j = e[i];
			if (dis[j] > distance + w[i]) {
				dis[j] = distance + w[i];
				heap.push({ dis[j],j });
			}
		}
	}
}
int main()
{
	memset(h, -1, sizeof(h));
	cin >> n >> m >> k;
	while (m--) {
		int a, b, c;
		scanf_s("%d%d%d", &a, &b, &c);
		add(a, b, c);
	}
	Dij();
	for (int i = 1; i <= n; i++) {
		if (dis[i] == 0x3f3f3f3f) {
			cout << pow(2, 31) - 1 << " ";
		}
		else {
			cout << dis[i] << " ";
		}
	}
	return 0;
}